class ThreadInfoPrinter {
    /**
     * 打印线程信息
     * @param {string} tag - 标识调用来源
     */
    static printThreadInfo(tag = "Default") {
        let threadInfo = {};

        if (typeof process !== 'undefined' && process.versions && process.versions.node) {
            // Node.js 环境
            // require('worker_threads');是如何拿到的？

            const { threadId } = require('worker_threads');
            threadInfo = {
                tag,
                environment: "Node.js",
                threadId: threadId || 0, // 主线程为 0，Worker 线程为其他值
                pid: process.pid,
                memoryUsage: process.memoryUsage(),
            };
        } else if (typeof Worker !== 'undefined') {
            // 浏览器环境（Web Worker）
            threadInfo = {
                tag,
                environment: "Browser (Web Worker)",
                threadId: self.name || "Main", // Web Worker 的名称，主线程为 "Main"
                memoryUsage: console.memory ? console.memory : "Not available",
            };
        } else {
            // 浏览器主线程
            threadInfo = {
                tag,
                environment: "Browser (Main Thread)",
                threadId: "Main",
                memoryUsage: console.memory ? console.memory : "Not available",
            };
        }

        console.log(`[${tag}] Thread Info:`, JSON.stringify(threadInfo, null, 2));
    }
}


ThreadInfoPrinter.printThreadInfo("afx");